package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.ExamManagement;
import cn.wolfcode.crm.mapper.ExamManagementMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IExamManagementService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ExamManagementServiceImpl implements IExamManagementService {
    @Autowired
    private ExamManagementMapper examManagementMapper;


    public PageResult query(QueryObject qo) {
        int total = examManagementMapper.query4Count(qo);
        if (total > 0) {
            return new PageResult(total, examManagementMapper.query4List(qo));
        }
        return new PageResult(total, Collections.EMPTY_LIST);
    }

    public List<ExamManagement> selectAll() {
        return examManagementMapper.selectAll();
    }

    public void pass(Long id) {
        ExamManagement examManagement = new ExamManagement();
        examManagement.setEmployeeName(UserContext.getCurrentUser().getUsername());
        examManagement.setId(id);
        examManagementMapper.pass(examManagement);
    }
    public void refuse(Long id) {
        ExamManagement examManagement = new ExamManagement();
        examManagement.setEmployeeName(UserContext.getCurrentUser().getUsername());
        examManagement.setId(id);
        examManagementMapper.refuse(examManagement);
    }

    public void save(ExamManagement examManagement) {
        examManagementMapper.insert(examManagement);
    }
    /*public HSSFWorkbook exportXls(QueryObject qo) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("employee");
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("真实姓名");
        row.createCell(2).setCellValue("电话");
        row.createCell(3).setCellValue("邮箱");
        row.createCell(4).setCellValue("部门");
        row.createCell(5).setCellValue("入职时间");
        row.createCell(6).setCellValue("状态");
        List<ExamManagement> employees = examManagementMapper.selectXls(qo);
        if (employees.size() > 0) {
            for (int i = 0; i < employees.size(); i++) {
                ExamManagement employee = employees.get(i);
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(employee.getUsername());
                row.createCell(1).setCellValue(employee.getRealname());
                row.createCell(2).setCellValue(employee.getTel());
                row.createCell(3).setCellValue(employee.getEmail());
                row.createCell(4).setCellValue(employee.getDept().getName());
                //设置导出的日期的格式
                HSSFCell cell = row.createCell(5);
                cell.setCellValue(new Date());
                HSSFCellStyle cellStyle = workbook.createCellStyle();

                HSSFDataFormat format = workbook.createDataFormat();

                cellStyle.setDataFormat(format.getFormat("yyyy年m月d日"));

                cell.setCellStyle(cellStyle);
                if (employee.getAdmin()) {
                    row.createCell(6).setCellValue("超级管理员");
                } else {
                    row.createCell(6).setCellValue("普通用户");
                }
            }
        }
        return workbook;
    }*/

    /*public void importXls(MultipartFile file) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        HSSFSheet sheet = workbook.getSheet("day01");
        int i = 0;
        for (Row cells : sheet) {
            if (i == 0) {
                i++;
                continue;
            }
            String username = cells.getCell(0).getStringCellValue();
            String realname = cells.getCell(1).getStringCellValue();
            String tel = cells.getCell(2).getStringCellValue();
            String email = cells.getCell(3).getStringCellValue();
            String dept_name = cells.getCell(4).getStringCellValue();
            Date hireDate = cells.getCell(5).getDateCellValue();
            ExamManagement employee = new ExamManagement();
            employee.setUsername(username);
            employee.setRealname(realname);
            employee.setTel(tel);
            employee.setHireDate(hireDate);
            String admin = cells.getCell(6).getStringCellValue();
            if("超级管理员".equals(admin)){
                employee.setAdmin(true);
            }else{
                employee.setAdmin(false);
            }
            examManagementMapper.insert(employee);
        }
    }*/
}
